package com.parenting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.parenting.entities.OrdersDO;
import com.parenting.enums.ResultEnum;
import com.parenting.exception.BaseException;
import com.parenting.mapper.OrdersMapper;
import com.parenting.model.IMPage;
import com.parenting.request.AlreadyRefundRequest;
import com.parenting.request.OrderSearchListCriteriaRequest;
import com.parenting.request.PendingRefundRequest;
import com.parenting.request.RefundRequest;
import com.parenting.request.platform.SearchCriteriaRequest;
import com.parenting.response.OrderListResponse;
import com.parenting.response.OrderStatisticsResponse;
import com.parenting.response.RefundListResponse;
import com.parenting.response.RefuseRefundResponse;
import com.parenting.response.platform.OrderPageListResponse;
import com.parenting.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单表  服务实现类
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-09-18
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, OrdersDO> implements OrdersService {


    @Override
    public IMPage<OrderListResponse> getOrderList(OrderSearchListCriteriaRequest request,String shopId) {
        LambdaQueryWrapper<OrdersDO> wrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getOrderNumber())){
            wrapper.like(OrdersDO::getOrderNumber,request.getOrderNumber());
        }
        if (StringUtils.isNotEmpty(request.getBuyerAccount())){
            wrapper.like(OrdersDO::getBuyerAccount,request.getBuyerAccount());
        }
        if (null!=request.getOrderState()){
            wrapper.eq(OrdersDO::getOrderState,request.getOrderState());
        }
        if (StringUtils.isNotEmpty(request.getOrderType())){
            wrapper.eq(OrdersDO::getOrderType,request.getOrderType());
        }
        if (StringUtils.isNotEmpty(request.getItemName())){
            wrapper.like(OrdersDO::getItemName,request.getItemName());
        }
        if (null!=request.getOrderStartTime()){
          wrapper.ge(OrdersDO::getOrderTime,request.getOrderStartTime());
        }
        if (null!=request.getOrderEndTime()){
            wrapper.le(OrdersDO::getOrderTime,request.getOrderEndTime());
        }
        if (null!= shopId){
            wrapper.le(OrdersDO::getShopId,shopId);
        }
        IPage<OrdersDO> ordersDOIPage = baseMapper.selectPage(request.getPage(), wrapper);
        List<OrderListResponse> ls=new ArrayList<>();
        if (ordersDOIPage.getRecords()!=null&&ordersDOIPage.getRecords().size()>0){
            for (OrdersDO o:ordersDOIPage.getRecords()) {
                OrderListResponse orderListResponse = BeanUtil.copyProperties(o, OrderListResponse.class);
                // 计算优惠总价
                if (null==o.getOriginalPrice()){
                    o.setOriginalPrice(0);
                }
                if (null==o.getItemConcessional()){
                    o.setItemConcessional(0);
                }
                if (null==o.getItemNumber()){
                    o.setItemNumber(0);
                }
                Integer  discount=(o.getOriginalPrice()-o.getItemConcessional())*o.getItemNumber();
                orderListResponse.setDiscount(discount);
                ls.add(orderListResponse);
            }
        }
        IMPage<OrderListResponse> page=new IMPage<>();
        page.setTotal(ordersDOIPage.getTotal());
        page.setCurrent(ordersDOIPage.getCurrent());
        page.setSize(ordersDOIPage.getSize());
        page.setRecords(ls);
        return page;
    }

    @Override
    public IMPage<RefundListResponse> getRefundList(RefundRequest refundRequest, String shopId) {
        LambdaQueryWrapper<OrdersDO> wrapper =new LambdaQueryWrapper<>();
        if (null==refundRequest.getOrderState()||refundRequest.getOrderState()==0){
            wrapper.ge(OrdersDO::getOrderState,1);
        }else{
            wrapper.eq(OrdersDO::getOrderState,refundRequest.getOrderState());
        }
        wrapper.eq(OrdersDO::getShopId,shopId);
        wrapper.orderByDesc(OrdersDO::getUpdatedTime);
        IPage<OrdersDO> ordersDOIPage = baseMapper.selectPage(refundRequest.getPage(), wrapper);
        IMPage  page=new IMPage();
        page.setSize(ordersDOIPage.getSize());
        page.setCurrent(ordersDOIPage.getCurrent());
        page.setRecords(ordersDOIPage.getRecords());
        page.setTotal(ordersDOIPage.getTotal());
        List<RefundListResponse> listResponses=new ArrayList<>();
        for (OrdersDO o:ordersDOIPage.getRecords()) {
            listResponses.add( BeanUtil.copyProperties(o,RefundListResponse.class));
        }
        page.setRecords(listResponses);
        return page;
    }

    @Override
    public void ByRefund(String orderId) {
        OrdersDO ordersDO = baseMapper.selectById(orderId);
        if (!ordersDO.getOrderType().equals("代金券")){
            throw  new BaseException(ResultEnum.ORDER_REFUSE_REFUND);
        }
        ordersDO.setOrderState(6);
        ordersDO.setUpdatedTime(new Date());
        // TODO 2、退款 调用 fein接口  实现真实退款



        baseMapper.updateById(ordersDO);

    }

    @Override
    public void RefuseRefund(RefuseRefundResponse refuseRefundResponse) {
        OrdersDO ordersDO = baseMapper.selectById(refuseRefundResponse.getOrderId());
        if (!ordersDO.getOrderType().equals("代金券")){
            throw  new BaseException(ResultEnum.ORDER_REFUSE_REFUND);
        }
        ordersDO.setOrderState(2);
        ordersDO.setPaymentStatus(4);
        ordersDO.setRemarks(refuseRefundResponse.getReason());
        ordersDO.setUpdatedTime(new Date());
        baseMapper.updateById(ordersDO);
    }

    @Override
    public IMPage<RefundListResponse> getPendingRefundList(PendingRefundRequest request, String shopId) {
        LambdaQueryWrapper<OrdersDO> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(OrdersDO::getOrderState,5);
        wrapper.eq(OrdersDO::getShopId,shopId);
        wrapper.orderByDesc(OrdersDO::getUpdatedTime);
        IPage<OrdersDO> ordersDOIPage = baseMapper.selectPage(request.getPage(), wrapper);
        IMPage  page=new IMPage();
        page.setSize(ordersDOIPage.getSize());
        page.setCurrent(ordersDOIPage.getCurrent());
        page.setRecords(ordersDOIPage.getRecords());
        page.setTotal(ordersDOIPage.getTotal());
        List<RefundListResponse> listResponses=new ArrayList<>();
        for (OrdersDO o:ordersDOIPage.getRecords()) {
            listResponses.add( BeanUtil.copyProperties(o,RefundListResponse.class));
        }
        page.setRecords(listResponses);
        return page;

    }

    @Override
    public IMPage<RefundListResponse> getAlreadyRefundList(AlreadyRefundRequest request, String shopId) {

        LambdaQueryWrapper<OrdersDO> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(OrdersDO::getOrderState,6).or().eq(OrdersDO::getOrderState,7);
        wrapper.eq(OrdersDO::getShopId,shopId);
        wrapper.orderByDesc(OrdersDO::getUpdatedTime);
        IPage<OrdersDO> ordersDOIPage = baseMapper.selectPage(request.getPage(), wrapper);
        IMPage  page=new IMPage();
        page.setSize(ordersDOIPage.getSize());
        page.setCurrent(ordersDOIPage.getCurrent());
        page.setRecords(ordersDOIPage.getRecords());
        page.setTotal(ordersDOIPage.getTotal());
        List<RefundListResponse> listResponses=new ArrayList<>();
        for (OrdersDO o:ordersDOIPage.getRecords()) {
            listResponses.add( BeanUtil.copyProperties(o,RefundListResponse.class));
        }
        page.setRecords(listResponses);
        return page;
    }

    @Override
    public IMPage<OrderPageListResponse> getPlatformOrderList(SearchCriteriaRequest request) {
        LambdaQueryWrapper<OrdersDO> wrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getOrderNumber())){
            wrapper.like(OrdersDO::getOrderNumber,request.getOrderNumber());
        }
        if (StringUtils.isNotEmpty(request.getBuyerAccount())){
            wrapper.like(OrdersDO::getBuyerAccount,request.getBuyerAccount());
        }
        if (null!=request.getOrderState()){
            wrapper.eq(OrdersDO::getOrderState,request.getOrderState());
        }
        if (StringUtils.isNotEmpty(request.getOrderType())){
            wrapper.eq(OrdersDO::getOrderType,request.getOrderType());
        }
        if (StringUtils.isNotEmpty(request.getItemName())){
            wrapper.like(OrdersDO::getItemName,request.getItemName());
        }
        if (null!=request.getOrderStartTime()){
            wrapper.ge(OrdersDO::getOrderTime,request.getOrderStartTime());
        }
        if (null!=request.getOrderEndTime()){
            wrapper.le(OrdersDO::getOrderTime,request.getOrderEndTime());
        }
        if (StringUtils.isNotEmpty(request.getShopId())){
            wrapper.eq(OrdersDO::getShopId,request.getShopId());
        }
        IPage<OrdersDO> ordersDOIPage = baseMapper.selectPage(request.getPage(), wrapper);
        List<OrderPageListResponse> ls=new ArrayList<>();
        if (ordersDOIPage.getRecords()!=null&&ordersDOIPage.getRecords().size()>0){
            for (OrdersDO o:ordersDOIPage.getRecords()) {
                OrderPageListResponse orderListResponse = BeanUtil.copyProperties(o, OrderPageListResponse.class);
                // 计算优惠总价
                if (null==o.getOriginalPrice()){
                    o.setOriginalPrice(0);
                }
                if (null==o.getItemConcessional()){
                    o.setItemConcessional(0);
                }
                if (null==o.getItemNumber()){
                    o.setItemNumber(0);
                }
                Integer  discount=(o.getOriginalPrice()-o.getItemConcessional())*o.getItemNumber();
                orderListResponse.setDiscount(discount);
                ls.add(orderListResponse);
            }
        }
        IMPage<OrderPageListResponse> page=new IMPage<>();
        page.setTotal(ordersDOIPage.getTotal());
        page.setCurrent(ordersDOIPage.getCurrent());
        page.setSize(ordersDOIPage.getSize());
        page.setRecords(ls);
        return page;
    }

    @Override
    public OrderStatisticsResponse situation(String type,String shopId) {
        return   baseMapper.situation(type,shopId);
    }


}
